<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>销售订单记录</title>
    <script type="text/javascript">
      var newSaleOrderWin;
      var editSaleOrderWin;
      var editSaleOrderAfterAuditWin
      var saleOrderId;
      var deliveryForSaleWin;
      var saleIntoPurchaseWin;
      var newSaleReturnedWin;
      var newSaleExchangeWin;
      var recListWin;
      var saleOrderDS;
      Ext.onReady(function(){
        var customer = Ext.create('Ext.form.ComboBox', {
          labelWidth:35,
          fieldLabel: '客户',
          store: customerAllInfoStore,
          allowBlank: false,
          blankText: "客户必须选择",
          emptyText: '请选择...',
          valueField: 'id',
          displayField: 'name',
          name: 'customer_id',
          loadingText: '搜索中...',
          minChars: 0,
          anchor: '100%',
          listConfig: {
            loadingText: 'Searching...',
            getInnerTpl: function() {
              return '<div style="float:left; width:70px">{number}</div><div style="float:left;width:230px">|{name}</div>| {phone}';
            }
          },
          pageSize: 10
        });

        var orderNumber = Ext.create('Ext.form.field.Text', {
          fieldLabel: '单号',
          name: 'order_number',
          anchor: '100%'
        });

        var auditingStatus = Ext.create('Ext.form.ComboBox', {
          fieldLabel: '审批状态',
          store: auditStatusStore,
          displayField:'text',
          typeAhead: true,
          queryMode: 'local',
          name: 'auditing_status',
          editable:false,
          valueField: 'value',
          anchor: '100%'
        });
        
        var beginDate = Ext.create('Ext.form.field.Date', {
          fieldLabel: '发货日期',
          name: 'begin_date',
          id: 'begin_date',
          format: 'Y-m-d', //格式化日期
          //disabledDays: [0,6],	//不让选择周六，周日
          //disabledDaysText: "周末要休息",
          invalidText: "您输入的日期格式不对，正确格式为2010-01-01",
          anchor: '100%',
          vtype: 'daterange',
          endDateField: 'end_date'          
        });
        
        var endDate = Ext.create('Ext.form.field.Date', {
          labelWidth: 30,
          fieldLabel: '至',
          name: 'end_date',
          id: 'end_date',
          format: 'Y-m-d', //格式化日期
          //disabledDays: [0,6],	//不让选择周六，周日
          //disabledDaysText: "周末要休息",
          invalidText: "您输入的日期格式不对，正确格式为2010-01-01",          
          vtype: 'daterange',
          startDateField: 'begin_date',
          anchor: '100%'
        });

        //产品
        var productCBB = Ext.create('Ext.form.ComboBox', {
          store: productStore,
          labelWidth:35,
          fieldLabel: '产品',
          emptyText: '请选择...',
          valueField: 'id',
          displayField: 'name',
          name: 'product_id',
          loadingText: '搜索中...',
          anchor: '100%',
          pageSize: 10,
          minChars: 0,
          listConfig: {
            loadingText: 'Searching...',
            getInnerTpl: function() {
              return '<div style="float:left; width:200px" data-qtip="{name}">{name:ellipsis(18)}</div><div style="float:left; width:110px">{number}</div>{is_suit}';
            }
          }
        });
        
        //销售员
        var employeeCBB = Ext.create('Ext.form.ComboBox', {
          store: employeeStore,
          fieldLabel: '销售员',
          emptyText: '请选择...',
          valueField: 'id',
          displayField: 'name',
          name: 'employee_id',
          loadingText: '搜索中...',
          anchor: '100%',
          //pageSize: 10,
          minChars: 0
        });
        
        //部门
        var department = Ext.create('Ext.form.ComboBox', {
          store: departmentStore,
          fieldLabel: '业绩部门',
          allowBlank: false,
          blankText: "部门必须选择",
          valueField: 'department_id',
          displayField: 'name',
          name: 'sell_dept_id',//如果不想提交displayField，则在这儿指定要提交的Key，value就是valueField．
          emptyText: '请选择...',          
          loadingText: '搜索中...',
          anchor: '100%',
          //pageSize: 10,          
          readOnly:false,
          minChars: 0
        });

        var cBeginDate = Ext.create('Ext.form.field.Date', {
          fieldLabel: '制单日期',
          name: 'c_begin_date',
          id: 'c_begin_date',
          format: 'Y-m-d', 
          invalidText: "您输入的日期格式不对，正确格式为2010-01-01",          
          anchor: '100%',
          vtype: 'daterange',
          endDateField: 'c_end_date'
        });

        var cEndDate = Ext.create('Ext.form.field.Date', {
          labelWidth: 30,
          fieldLabel: '至',
          name: 'c_end_date',
          id: 'c_end_date',
          format: 'Y-m-d', //格式化日期
          //disabledDays: [0,6],	//不让选择周六，周日
          //disabledDaysText: "周末要休息",
          invalidText: "您输入的日期格式不对，正确格式为2010-01-01",          
          anchor: '100%',
          vtype: 'daterange',
          startDateField: 'c_begin_date'
        });
        
        var searchButton = Ext.create('Ext.Button', {
          text: '搜索',
          iconCls: 'finding',
          handler: function(){
            saleOrderDS.removeAll();            
            saleOrderDS.getProxy().extraParams = queryFormPanel.getForm().getFieldValues();
            toolBar.moveFirst();            
          }
        });

        var clearButton = Ext.create('Ext.Button', {
          text: '清空',
          width:50,
          handler: function(){
            queryFormPanel.form.reset();
          }
        });

        var queryFormPanel = Ext.create('Ext.form.Panel', {
          fieldDefaults: {
            labelWidth: 60,
            labelAlign: 'right'
          },
          width: screenWidth-165,
          frame : true,
          border:false,
          items: [{
              xtype: 'fieldset',
              title: '查询条件',
              defaults: {anchor: '100%'},
              items: [{
                  xtype: 'container',
                  anchor: '100%',
                  layout:'column',
                  items: [{
                      columnWidth:.12,
                      xtype: 'container',
                      layout: 'anchor',
                      items: [orderNumber]
                    }, {
                      columnWidth:.15,
                      xtype: 'container',
                      layout: 'anchor',
                      items: [cBeginDate]
                    }, {
                      columnWidth:.12,
                      layout: 'anchor',
                      xtype: 'container',
                      items: [cEndDate]
                    }, {
                      columnWidth:.39,
                      layout: 'anchor',
                      xtype: 'container',                      
                      items: [customer]
                    },{
                      columnWidth:.13,
                      xtype: 'container',
                      layout: 'anchor',
                      items: [employeeCBB]
                    }, {
                      columnWidth: .09,
                      layout: 'anchor',
                      xtype: 'container',
                      items: [searchButton]
                    }]
                },{
                  xtype: 'container',
                  layout: 'column',
                  items: [{
                      columnWidth: .12,
                      xtype: 'container',
                      layout: 'anchor',
                      items: [auditingStatus]
                    }, {
                      columnWidth: .15,
                      xtype: 'container',
                      layout: 'anchor',
                      items: [beginDate]
                    }, {
                      columnWidth: .12,
                      xtype: 'container',
                      layout: 'anchor',
                      items: [endDate]
                    }, {
                      columnWidth: .39,
                      xtype: 'container',
                      layout: 'anchor',
                      items: [productCBB]
                    }, {
                      columnWidth: .13,
                      xtype: 'container',
                      layout: 'anchor',
                      items: [department]
                    }, {
                      columnWidth: .09,
                      xtype: 'container',
                      blayout: 'anchor',
                      items: [clearButton]
                    }]
                }]
            }]
        });

        saleOrderDS = Ext.create('Ext.data.Store', {
          //autoDestroy : true,
          model : 'SaleOrderModel',
          proxy : {
            type : 'ajax',
            url : '/sale_order/getAllSaleOrders',
            reader : {              
              type : 'json',
              root : 'root',
              totalProperty : 'totalProperty'// 数据集记录总数
            }
          },
          pageSize : pageSize,
          autoLoad : true
        });
        
        var toolBar = Ext.create('Ext.toolbar.Paging', {
          pageSize: pageSize,
          store: saleOrderDS,
          displayInfo: true,
          displayMsg: "显示第 {0} 条到 {1} 条记录，一共 {2} 条",
          emptyMsg: "没有记录",
          items:["-",{
              text: '新增',
              iconCls: 'addItem',
              handler : function(){
                newSaleOrderWin = Ext.create('Ext.window.Window', {
                  layout: 'fit',
                  width: 1067,
                  height: 505,
                  closeAction: 'destroy',
                  plain: true,
                  modal: true,
                  constrain:true,
                  //modal: true,
                  title: '新增销售订单',
                  autoLoad: {
                    url: "/sale_order/newSaleOrder",
                    scripts: true
                  }
                });
                newSaleOrderWin.on("destroy",function(){
                  saleOrderDS.load();
                });
                newSaleOrderWin.resizable = false;
                newSaleOrderWin.show();
              }
            },"-",{
              text: '编辑',
              iconCls: 'editItem',
              handler : function(){
                var records = saleOrderGrid.getSelectionModel().getSelection();
                if(records.length==0){
                  Ext.MessageBox.show({
                    title: '提示信息',
                    msg: "请先选中一条记录后，再编辑。",
                    buttons: Ext.MessageBox.OK,
                    icon: Ext.MessageBox.WARNING
                  });
                }else{
                  saleOrderId = records[0].get('id');
                  if(records[0].get('auditing_status').indexOf('申请') < 0 && records[0].get('auditing_status')=='' || records[0].get('auditing_status') == '驳回新增'){
                    editSaleOrderWin = Ext.create('Ext.window.Window', {
                      layout: 'fit',
                      width: 1067,
                      height: 520,
                      closeAction: 'destroy',
                      plain: true,
                      modal: true,
                      constrain:true,
                      //modal: true,
                      title: '编辑销售订单',
                      autoLoad: {
                        url: "/sale_order/editSaleOrder?id="+saleOrderId,
                        scripts: true,
                        callback: function(options, success, response, request)
                        {
                          if (response.responseText === "NoPrivilege"){
                            editSaleOrderWin.close();
                          }
                        }
                      }
                    });
                    editSaleOrderWin.on("destroy",function(){
                      saleOrderDS.load();
                    });
                    editSaleOrderWin.resizable = false;
                    editSaleOrderWin.show();
                  }else if (records[0].get('auditing_status').indexOf('申请') > -1 || records[0].get('auditing_status') == '通过撤消'){
                    Ext.MessageBox.alert('提示信息','正在申请、已经撤消的销售订单，不能编辑。');
                  }else if(records[0].get('auditing_status') == '通过新增' || records[0].get('auditing_status') == '通过变更' || records[0].get('auditing_status') == '驳回变更'|| records[0].get('auditing_status') == '驳回撤消'){
                    Ext.MessageBox.confirm('警告',"通过审批的'新增'或'更新'的销售订单在编辑后，要从新通过审批，你确认要编辑此订单吗？",function (button){
                      if(button=='yes'){
                        editSaleOrderAfterAuditWin = Ext.create('Ext.window.Window', {
                          layout: 'fit',
                          width: 1097,
                          height: 510,
                          closeAction: 'destroy',
                          plain: true,
                          modal: true,
                          constrain:true,
                          //modal: true,
                          title: '审批后，调整销售订单',
                          autoLoad: {
                            url: "/sale_order/editSaleOrderAfterAudit?id="+saleOrderId,
                            scripts: true,
                            callback: function(options, success, response, request)
                            {
                              if (response.responseText === "NoPrivilege"){
                                editSaleOrderAfterAuditWin.close();
                              }
                            }
                          }
                        });
                        editSaleOrderAfterAuditWin.on("destroy",function(){
                          saleOrderDS.load();
                        });
                        editSaleOrderAfterAuditWin.resizable = false;
                        editSaleOrderAfterAuditWin.show();
                      }
                    });
                  }
                }
              }
            },'-',{
              text: '撤消',
              iconCls: 'cancel',
              handler : function(){
                var records = saleOrderGrid.getSelectionModel().getSelection();
                if (records.length == 0) {
                  return false;
                }else{
                  if(records[0].get('deal_with_status') != '已撤消' && records[0].get('auditing_status').indexOf('申请') < 0 ){
                    var orderNumber = records[0].get('order_number')
                    saleOrderId = records[0].get('id');
                    Ext.MessageBox.confirm("提示","你确认要申请撤消订单号为： \""+orderNumber+"\" 的销售订单吗？",function(btn){
                      if(btn=="yes"){
                        Ext.Ajax.request({
                          url : '/sale_order/applyToCancel?id=' + saleOrderId,
                          success : function (result, request) {
                            saleOrderDS.load();
                          },
                          failure : function (result, request){
                            Ext.MessageBox.show({
                              title: '消息',
                              msg: "通讯失败，请从新操作",
                              buttons: Ext.MessageBox.OK,
                              icon: Ext.MessageBox.WARNING
                            });
                          },
                          method : 'GET'
                        });
                      }else{
                        //alert("no");
                      }
                    });
                  }else{
                    Ext.MessageBox.alert('提示信息','已经撤消或正于申请状态的各类订单，不能撤消。');
                  }
                }
              }
            },'-',{
              text: '打印销售凭据',
              iconCls: 'print',
              handler : function(){
                var records = saleOrderGrid.getSelectionModel().getSelection();
                if (records.length == 0) {
                  return false;
                }else{
                  if(records[0].get('deal_with_status') != '已撤消'){
                    this.disable();
                    saleOrderId = records[0].get('id');
                    //LODOP.SET_PRINT_PAGESIZE(1,0,0,'A5')
                    LODOP.PRINT_INIT("");
                    LODOP.SET_PRINT_PAGESIZE(1,"210mm","148mm","");
                    //LODOP.SET_PRINT_PAGESIZE(1,"210mm","297mm","");
                    //LODOP.SET_PRINT_PAGESIZE(1,"148mm","210mm","")
                    
                    LODOP.ADD_PRINT_URL(20,'5%',"90%","100%",'http://'+window.location.host+'/sale_order/sale_order_print_header?id='+saleOrderId);
                    LODOP.SET_PRINT_STYLEA(0,"ItemType",1);
                    LODOP.ADD_PRINT_TBURL(140,'5%',"90%","43%",'http://'+window.location.host+'/sale_order/sale_order_print_content?id='+saleOrderId);
                    LODOP.SET_PRINT_STYLEA(0,"ItemType",4);
                    LODOP.ADD_PRINT_URL(415,'5%',"90%","100%",'http://'+window.location.host+'/sale_order/sale_order_print_footer?id='+saleOrderId);
                    LODOP.SET_PRINT_STYLEA(0,"ItemType",1);
                    LODOP.ADD_PRINT_TEXT(485,'15%',"80%","8%","第一联：成本（白） 第二联：财务（绿） 第三联：提货（粉） 第四联：客户（黄） 第五联：销售（兰）");
                    LODOP.SET_PRINT_STYLEA(0,"ItemType",1);
                    LODOP.ADD_PRINT_TEXT(505,'35%',"90%","8%","页号：第#页/共&页 打印日期："+getDate());
                    LODOP.SET_PRINT_STYLEA(0,"ItemType",2);
                    
                    LODOP.PREVIEW();
                    this.enable();
                    //window.open('/test/sale_order_print?id='+saleOrderId,'_blank','width=0,height=0,toolbar=no,menubar=no,location=no');
                  }
                }
              }
            }]
        })
        //生成要订单的产品的Grid
        var saleOrderGrid = Ext.create('Ext.grid.Panel', {
          title: '销售订单列表',
          //store: saleOrderDS,
          store: saleOrderDS,
          //columns : saleOrderCM,
          columns: [Ext.create('Ext.grid.RowNumberer'),
            {
              text     : 'DB',
              dataIndex: 'id',
              width: 50
            },{
              header: '公司',
              dataIndex: 'company_name',
              width: 50
            },{
              header: '销售订单单号',
              dataIndex: 'order_number',
              width: 210
            },{
              header: '客户',
              dataIndex: 'customer_name',
              width: 160
            },{
              header: '总金额',
              dataIndex: 'amount',
              align: 'right',
              renderer: moneyPrivilegeRender,
              width: 85
            },{
              header: '已收金额',
              dataIndex: 'rec_amount',
              align: 'right',
              renderer: moneyPrivilegeRender,
              width: 75
            },{
              header: '发货日期',
              dataIndex: 'action_date',
              renderer: Ext.util.Format.dateRenderer('Y-m-d'),width: 80
            },{
              header: '销售员',
              dataIndex: 'sales_name',
              width: 60
            },{
              header: '业绩部门',
              dataIndex: 'sell_department_name',
              width: 75
            },{
              header: '操作员',
              dataIndex: 'employee_name',
              width: 55
            },{
              header: '部门',
              dataIndex: 'department_name',
              width: 80
            },{
              header: '有退货?',
              dataIndex: 'sale_returneds',
              width: 65,
              renderer: yesOrNoRender
            },{
              header: '授信折扣',
              dataIndex: 'unsafe_discount',
              width: 65,
              renderer: yesOrNoRender
            },{
              header: '审批状态',
              dataIndex: 'auditing_status',
              width: 65,
              renderer: auditingStatusRender
            },{
              header: '审批者',
              dataIndex: 'auditor_name',
              width: 55
            },{
              header: '审批意见',
              dataIndex: 'audit_opinion',
              width: 150
            },{
              header: '审批时间',
              dataIndex: 'auditing_date',renderer: Ext.util.Format.dateRenderer('Y-m-d H:i'),width: 115
            },{
              header: '处理状态',
              dataIndex: 'deal_with_status',
              renderer: dealWithStatusRender,
              width: 70
            },{
              header: '采购状态',
              dataIndex: 'purchase_status',
              width: 70
            },{
              header: '出库状态',
              dataIndex: 'store_status',
              renderer: dealWithStatusRender,
              width: 70
            },{
              header: '仓库操作员',
              dataIndex: 'delivery_employee_name',
              width: 70
            },{
              header: '销售类型',
              dataIndex: 'sale_type',
              width: 70
            },{
              header: '销售策略',
              dataIndex: 'sales_strategy',
              width: 80
            },{
              header: '备注',
              dataIndex: 'description',
              width: 230
            },{
              header: '出库说明',
              dataIndex: 'delivery_desc',
              width: 230
            },{
              header: '生成日期',
              dataIndex: 'created_at',
              renderer: Ext.util.Format.dateRenderer('Y-m-d H:i'),width: 115
            },{
              header: '修改日期',
              dataIndex: 'updated_at',
              renderer: Ext.util.Format.dateRenderer('Y-m-d H:i'),width: 115
            }],
          selModel : Ext.create('Ext.selection.CheckboxModel'),
          loadMask: true,
          //renderTo: 'order_sale_list_grid',
          width: screenWidth-165,
          height: 315,
          frame: true,
          //title: '进货品种列表',
          //plain:true,
          //plugins: checkColumn,
          tbar: toolBar          
        });

        //Ext.view.View this, Ext.data.Model record, HTMLElement item, Number index, Ext.EventObject e, Object options
        saleOrderGrid.on('itemdblclick', function(gridPanel, record,item,index,e,options){
          saleOrderId = record.get('id');
          var saleOrderNumber = record.get('order_number');
          //saleOrderDS.load();
          saleOrderDetailDS.removeAll();
          saleOrderDetailDS.load({params:{'sale_order_id': saleOrderId}});
          saleOrderDetailGrid.setTitle("销售订单："+saleOrderNumber+"，的详细信息");
        });

        //itemcontextmenu,itemcontextmenu，containercontextmenu
        //Ext.view.View this, Ext.data.Model record, HTMLElement item, Number index, Ext.EventObject e, Object options 
        saleOrderGrid.on('itemcontextmenu', function(grid, record,item,index,e,options){
          //初始化所有右键菜单为不可用
          deliveryForSaleItem.setDisabled(true);
          //recItem.setDisabled(true);
          intoPurchaseItem.setDisabled(true);
          returnedItem.setDisabled(true);
          finishPurchaseStatusItem.setDisabled(true);
          cancelPurchaseStatusItem.setDisabled(true);
          
          saleOrderId = record.get('id');
          currentOrderNumber = record.get('order_number');
          var auditingStatus = record.get('auditing_status');
          var purchaseStatus = record.get('purchase_status');
          var intoPurchase = record.get('is_transformed_into_purchase');
          if (auditingStatus.indexOf("通过") > -1){
            deliveryForSaleItem.setDisabled(false);
            recItem.setDisabled(false);
          }else {
            deliveryForSaleItem.setDisabled(true);
          }
          
          if (intoPurchase=="否" && auditingStatus.indexOf("通过") > -1){
            intoPurchaseItem.setDisabled(false);
          }else {
            intoPurchaseItem.setDisabled(true);
          }

          //只要通过审批就可以退货，不一定非要转为采购订单以后，有时候库里有货，有销售订单，直接就可以销售。
          if (auditingStatus.indexOf("通过") > -1){
            returnedItem.setDisabled(false);
          }else{
            returnedItem.setDisabled(true);
          }

          if (purchaseStatus == ""){
            finishPurchaseStatusItem.setDisabled(false);          
          }else{
            cancelPurchaseStatusItem.setDisabled(false);
          }
          
          e.preventDefault();
          rightClick.showAt(e.getXY());
        });
        
        //############ 订单-右键菜单 ##########
        var deliveryForSaleItem = Ext.create('Ext.menu.Item', {
          text: '【相关出库单信息】',
          //iconCls: 'album',
          handler: rightMenuDeliveryForSaleFn
        });

        function rightMenuDeliveryForSaleFn(){
          deliveryForSaleWin = Ext.create('Ext.window.Window', {
            layout: 'fit',
            width:1198,
            height: 600,
            closeAction: 'destroy',
            plain: true,
            modal: true,
            constrain:true,
            draggable: false,
            //modal: true,
            title: '销售订单\"'+currentOrderNumber+'\"相关的出库单信息',
            autoLoad: {
              url: "/sale_order/deliveryForSaleList?id="+saleOrderId,
              scripts: true
            }
          });
          deliveryForSaleWin.on("destroy",function(){
            saleOrderDS.load();
          });
          deliveryForSaleWin.resizable = false;
          deliveryForSaleWin.show();
        }

        var returnedForSaleItem = Ext.create('Ext.menu.Item', {
          text: '【相关退货订单信息】',
          //iconCls: 'album',
          handler: rightMenuReturnedForSaleFn
        });


        function rightMenuReturnedForSaleFn(){
          returnedForSaleWin = Ext.create('Ext.window.Window', {
            layout: 'fit',
            width:998,
            height: 600,
            closeAction: 'destroy',
            plain: true,
            modal: true,
            constrain:true,
            draggable: false,
            //modal: true,
            title: '销售订单\"'+currentOrderNumber+'\"相关的退货订单信息',
            autoLoad: {
              url: "/sale_order/returnedForSaleList?id="+saleOrderId,
              scripts: true
            }
          });
          returnedForSaleWin.on("destroy",function(){
            saleOrderDS.load();
          });
          returnedForSaleWin.resizable = false;
          returnedForSaleWin.show();
        }
        
        var intoPurchaseItem = Ext.create('Ext.menu.Item', {
          text: '【转为采购订单】',
          //iconCls: 'album',
          handler: rightMenuIntoPurchaseFn
        });

        function rightMenuIntoPurchaseFn(){
          saleIntoPurchaseWin = Ext.create('Ext.window.Window', {
            layout: 'fit',
            width: 860,
            height: 495,
            closeAction: 'destroy',
            plain: true,
            modal: true,
            constrain:true,
            draggable: false,
            //modal: true,
            title: '转换销售订单：\"'+currentOrderNumber+'\"为采购订单',
            autoLoad: {
              url: "/purchase_order/newPurchaseFromSale?id="+saleOrderId,
              scripts: true
            }
          });
          saleIntoPurchaseWin.on("destroy",function(){
            saleOrderDS.load();
          });
          saleIntoPurchaseWin.resizable = false;
          saleIntoPurchaseWin.show();
        }

        var recItem = Ext.create('Ext.menu.Item', {
          text: '【收款记录】',
          //iconCls: 'album',
          handler: rightMenuRecFn
        });

        function rightMenuRecFn(){
          recListWin = Ext.create('Ext.window.Window', {
            layout: 'fit',
            width: 1100,
            height: 475,
            closeAction: 'destroy',
            plain: true,
            modal: true,
            constrain:true,
            draggable: false,
            //modal: true,
            title: '销售订单：\"'+currentOrderNumber+'\"的收款记录',
            autoLoad: {
              url: "/receivables/receivablesListForSale?id="+saleOrderId,
              scripts: true
            }
          });
          recListWin.on("destroy",function(){
            saleOrderDS.load();
          });
          recListWin.resizable = false;
          recListWin.show();
        }

        var returnedItem = Ext.create('Ext.menu.Item', {
          text: '【销售退货】',
          //iconCls: 'album',
          handler: rightMenuReturnedFn
        });

        function rightMenuReturnedFn(){
          newSaleReturnedWin = Ext.create('Ext.window.Window', {
            layout: 'fit',
            width: 1065,
            height: 500,
            closeAction: 'destroy',
            plain: true,
            modal: true,
            constrain:true,
            draggable: false,
            //modal: true,
            title: '销售订单：\"'+currentOrderNumber+'\"的退货记录',
            autoLoad: {
              url: "/sale_returned/newSaleReturned?id="+saleOrderId,
              scripts: true
            }
          });
          newSaleReturnedWin.on("destroy",function(){
            saleOrderDS.load();
          });
          newSaleReturnedWin.resizable = false;
          newSaleReturnedWin.show();
        }

        var exchangeItem = Ext.create('Ext.menu.Item', {
          text: '【销售换货】',
          //iconCls: 'album',
          handler: rightMenuExchangeFn
        });

        function rightMenuExchangeFn(){
          newSaleExchangeWin = Ext.create('Ext.window.Window', {
            layout: 'fit',
            width: 1000,
            height: 500,
            closeAction: 'destroy',
            plain: true,
            modal: true,
            constrain:true,
            draggable: false,
            //modal: true,
            title: '销售订单：\"'+currentOrderNumber+'\"的换货记录',
            autoLoad: {
              url: "/sale_exchange/newSaleExchange?id="+saleOrderId,
              scripts: true
            }
          });
          newSaleExchangeWin.on("destroy",function(){
            saleOrderDS.load();
          });
          newSaleExchangeWin.resizable = false;
          newSaleExchangeWin.show();
        }
        
        var saleOrderHxItem = Ext.create('Ext.menu.Item', {
          text: '【变更历史】',
          //iconCls: 'album',
          handler: rightMenusaleOrderHxFn
        });

        function rightMenusaleOrderHxFn(){
          saleOrderHxWin = Ext.create('Ext.window.Window', {
            layout: 'fit',
            width: 960,
            height: 550,
            closeAction: 'destroy',
            plain: true,
            modal: true,
            constrain:true,
            draggable: false,
            //modal: true,
            title: '销售订单：\"'+currentOrderNumber+'\"的变更记录',
            autoLoad: {
              url: "/sale_order/hxListForSale?id="+saleOrderId,
              scripts: true
            }
          });
          saleOrderHxWin.on("destroy",function(){
            saleOrderDS.load();
          });
          saleOrderHxWin.resizable = false;
          saleOrderHxWin.show();
        }

        var finishPurchaseStatusItem = Ext.create('Ext.menu.Item', {
          text: '【设置采购完毕状态】',
          //iconCls: 'album',
          handler: rightMenuFinishPurchaseStatusFn
        });

        function rightMenuFinishPurchaseStatusFn(){
          Ext.Ajax.request({
            url : '/sale_order/setupPurchaseStatus',
            success : function (result, request) {
              saleOrderDS.load();
            },
            failure : function (result, request){
              Ext.MessageBox.show({
                title: '消息',
                msg: "通讯失败，请从新操作",
                buttons: Ext.MessageBox.OK,
                icon: Ext.MessageBox.WARNING
              });
            },
            method : 'GET',
            params : {
              id : saleOrderId,
              purchase_status : 'finish'
            }
          });
        }

        var cancelPurchaseStatusItem = Ext.create('Ext.menu.Item', {
          text: '【取消采购完毕状态】',
          //iconCls: 'album',
          handler: rightMenuCancelPurchaseStatusFn
        });

        function rightMenuCancelPurchaseStatusFn(){
          Ext.Ajax.request({
            url : '/sale_order/setupPurchaseStatus',
            success : function (result, request) {
              saleOrderDS.load();
            },
            failure : function (result, request){
              Ext.MessageBox.show({
                title: '消息',
                msg: "通讯失败，请从新操作",
                buttons: Ext.MessageBox.OK,
                icon: Ext.MessageBox.WARNING
              });
            },
            method : 'GET',
            params : {
              id : saleOrderId
            }
          });
        }
        
        var rightClick = Ext.create('Ext.menu.Menu', {
          items: [deliveryForSaleItem,'-',returnedForSaleItem,'-',recItem,'-',returnedItem,'-',saleOrderHxItem,'-',finishPurchaseStatusItem,cancelPurchaseStatusItem]
        });

        //--------------------------销售订单详细，双击后可显示数据----------------
        //销售订单Grid Store Detail
        var saleOrderDetailDS = Ext.create('Ext.data.Store', {
          //autoDestroy : true,
          model : 'SaleOrderDetailModel',
          proxy : {
            type : 'ajax',
            url : '/sale_order/getSaleOrderDetailsBalanceById',
            reader : {
              type : 'json',
              root : 'root',
              totalProperty : 'totalProperty'// 数据集记录总数
            }
          }
        });
        
        //生成要订单的产品的Grid
        var saleOrderDetailGrid = Ext.create('Ext.grid.Panel', {
          title:'订单详细列表',
          store: saleOrderDetailDS,
          columns : [Ext.create('Ext.grid.RowNumberer'),{              
              text: 'DB',
              dataIndex: 'id',
              width: 60
            },{
              header: '产品名称',
              dataIndex: 'product_name',
              width: 270
            },{
              header: '编号',dataIndex: 'product_number',width: 100
            },{
              header: '规格',
              dataIndex: 'spec',
              width: 70
            },{
              header: '数量',
              dataIndex: 'quantity',
              align: 'right',
              width: 60
            },{
              header: '单价',
              dataIndex: 'unit_price',
              align: 'right',
              //renderer: moneyPrivilegeRender,
              width: 70
            },{
              header: '折扣',
              dataIndex: 'discount',
              renderer: moneyPrivilegeRender,
              renderer: Ext.util.Format.numberRenderer('0,000.000000'),
              width: 70
            },{
              header: '总价',
              dataIndex: 'amount',
              align: 'right',
              renderer: moneyPrivilegeRender,
              width: 80
            },{
              header: '套装或单品',
              dataIndex: 'is_suit',
              width: 70
            },{
              header: '出库差比',
              dataIndex: 'balance_quantity',
              align: 'right',
              width: 80
            },{
              header: '备注',
              dataIndex: 'description',
              width: 170
            }],
          selModel : Ext.create('Ext.selection.CheckboxModel'),
          loadMask: true,
          width: screenWidth-165,
          height: dgHeight,
          frame: true
          //title: '进货品种列表',
          //plain:true,
          //plugins: checkColumn,
        });
        
        //总的panel
        var workbenchPanel = Ext.create('Ext.panel.Panel', {
          width: screenWidth-160,
          height: totalHeight,autoScroll:true,
          frame:false,
          layout: {
            type: 'table',
            columns:1
          },
          items:[queryFormPanel,saleOrderGrid,saleOrderDetailGrid]
        });
        workbenchPanel.render("order_sale_list_workbench");
      })
    </script>
  </head>
  <body>
    <div id="order_sale_list_workbench"></div>    
  </body>
</html>